package com.topboya.linker.ui.presenter;

import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.topboya.linker.AppConst;
import com.topboya.linker.R;
import com.topboya.linker.api.ApiRetrofit;
import com.topboya.linker.exception.ServerException;
import com.topboya.linker.ui.activity.LoginActivity;
import com.topboya.linker.ui.activity.xMainActivity;
import com.topboya.linker.ui.base.Presenter.BasePresenter;
import com.topboya.linker.ui.base.activity.BaseActivity;
import com.topboya.linker.ui.view.GuideLoginView;
import com.topboya.linker.utils.CacheUtils;
import com.topboya.linker.utils.LogUtils;
import com.topboya.linker.utils.UIUtils;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class GuideLoginPrestener extends BasePresenter<GuideLoginView> {

    private static final String TAG = "GuideLoginPrestener";
    public GuideLoginPrestener(BaseActivity context) {
        super(context);
    }

    public void login(String phone,String pwd) {

        if (TextUtils.isEmpty(phone)) {
            UIUtils.showToast(UIUtils.getString(R.string.phone_not_empty));
            return;
        }
        if (TextUtils.isEmpty(pwd)) {
            UIUtils.showToast(UIUtils.getString(R.string.password_not_empty));
            return;
        }
        mContext.showWaitingDialog(UIUtils.getString(R.string.please_wait));
        ApiRetrofit.getInstance().authorize(phone, pwd, "m", null)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(loginResponse -> {
                    String success = loginResponse.getMeta().getSuccess();
                    String message = loginResponse.getMeta().getMessage();
                    String coderesult=loginResponse.getMeta().getCode();
                    mContext.hideWaitingDialog();
                    if (null != success && Boolean.parseBoolean(success)) {

//                        UserCache.save(loginResponse.getData().getUser().getId(), phone, loginResponse.getResult().getToken());
                        String token = loginResponse.getData().getToken();
                        AppConst.TOKEN = token;
                        AppConst.USERID =loginResponse.getData().getUser().getId();
                        AppConst.OFFICEID = loginResponse.getData().getUser().getOfficeId();
                        CacheUtils.putLoginname(mContext,AppConst.PHONE,phone);
                        CacheUtils.putLoginpw(mContext,AppConst.PW,pwd);
                        mContext.jumpToActivityAndClearTask(xMainActivity.class);
                        mContext.finish();

                    } else  {

                        mContext.jumpToActivityAndClearTask(LoginActivity.class);

                    }
                }, this::loginError);
    }

    private void loginError(Throwable throwable) {

        LogUtils.v(throwable.getLocalizedMessage());
//        UIUtils.showToast(throwable.getLocalizedMessage());
        LogUtils.v(TAG + "loginError:" + throwable.getLocalizedMessage());
        mContext.hideWaitingDialog();
    }
}
